package CDLibraryV2;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JList;

import CDLibrary.CDBean;

import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BuscarReg extends JDialog {

	// private final JPanel contentPanel = new JPanel();
	private static final long serialVersionUID = 1L;
	private CD ventanaPadre;
	private JButton jbtAcceptar;
	private JButton jbtCancelar;
	private JButton jbtBorrar;
	private JList jlsListaCDs;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// try {
	// BuscarReg dialog = new BuscarReg();
	// dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	// dialog.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	/**
	 * Create the dialog.
	 */
	private void initGUI() {
		try {
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setResizable(false);
		

			getContentPane().setLayout(null);
			this.setTitle("Buscar Registres");
			pack();
			setSize(327, 327);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void construeixGUI() {

		JScrollPane jScrollPane1 = new JScrollPane();
		jScrollPane1.setBounds(17, 21, 247, 220);
		getContentPane().add(jScrollPane1);
		jlsListaCDs = new JList();
		jScrollPane1.setViewportView(jlsListaCDs);

		jbtAcceptar = new JButton("Acceptar");
		jbtAcceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int i = jlsListaCDs.getSelectedIndex();
				if (i >= 0)
					ventanaPadre.setRegDatos(i);
			}
		});
		jbtAcceptar.setMnemonic('A');
		jbtAcceptar.setBounds(311, 60, 89, 23);
		getContentPane().add(jbtAcceptar);

		jbtCancelar = new JButton("Cancelar");
		jbtCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tancarFinestra();
			}
		});
		jbtCancelar.setBounds(311, 105, 89, 23);
		getContentPane().add(jbtCancelar);

		jbtBorrar = new JButton("Borrar");
		jbtBorrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int i = jlsListaCDs.getSelectedIndex();
				if (i >= 0) {

					ventanaPadre.CDs.remove(i);
					jlsListaCDs.clearSelection();
					actualitzarLlista();
					ventanaPadre.buidarFormulari();
				}

			}
		});
		jbtBorrar.setMnemonic('B');
		jbtBorrar.setBounds(311, 156, 89, 23);
		getContentPane().add(jbtBorrar);
	}

	protected void tancarFinestra() {
		this.setVisible(false);
		this.dispose();
	}

	public void actualitzarLlista() {
		jlsListaCDs.setListData(ventanaPadre.getCDs());
	}

	public BuscarReg(JFrame frame) {
		super(frame);
		
		setResizable(false);
		initGUI();
		ventanaPadre = (CD) frame;
		getContentPane().setLayout(null);
		construeixGUI();
		jlsListaCDs.setListData(ventanaPadre.getCDs());
	}
	
}
